<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search_div">
                <div class="add">
                    <Button v-if="role < 3 " size="default" icon="md-add" type="primary" @click="add">新增目标金额</Button>
                </div>

                <Form :model="search" class="search_box" inline label-position="right">
                    <FormItem label="部门：">
                        <Select class="option" v-model="search.department">
                            <Option value="全部">全部</Option>
                            <Option v-for="item in demList " :value="item" :key="item">{{ item }}</Option>
                        </Select>
                    </FormItem>

                    <FormItem label="年份：">
                        <Input v-model="search.year" placeholder="输入年份" class="FormItemInput"></Input>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                    </FormItem>
                </Form>
            </div> <!-- end search -->

            <Table ref="table" :loading="loading" stripe :columns="columns1" :data="data1"></Table>
            <Page :total="total" :current="search.page" show-sizer show-total :page-size="search.rows" @on-page-size-change="getListByPageSize" @on-change="getListByPageNum"> </Page>
        </Card>

        <Modal v-model="modal" :title=" formItem.wdtmid? '修改目标金额':'新增目标金额'" :mask-closable="false" :styles="{top:'20px'}">
            <Form :label-width="140" :model="formItem">

                <FormItem label="年份：">
                    <Input v-model="formItem.year" placeholder="输入年份" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="部门：">
                    <Select style="width:15vw" v-model="formItem.department">
                        <Option v-for="item in demList " :value="item" :key="item">{{ item }}</Option>
                    </Select>
                </FormItem>

                <FormItem label="一月：">
                    <Input v-model="formItem.month_target_money[1]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="二月：">
                    <Input v-model="formItem.month_target_money[2]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="三月：">
                    <Input v-model="formItem.month_target_money[3]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="四月：">
                    <Input v-model="formItem.month_target_money[4]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="五月：">
                    <Input v-model="formItem.month_target_money[5]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="六月：">
                    <Input v-model="formItem.month_target_money[6]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="七月：">
                    <Input v-model="formItem.month_target_money[7]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="八月：">
                    <Input v-model="formItem.month_target_money[8]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="九月：">
                    <Input v-model="formItem.month_target_money[9]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="十月：">
                    <Input v-model="formItem.month_target_money[10]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="十一月：">
                    <Input v-model="formItem.month_target_money[11]" style="width:15vw"></Input>
                </FormItem>

                <FormItem label="十二月：">
                    <Input v-model="formItem.month_target_money[12]" style="width:15vw"></Input>
                </FormItem>

            </Form>
            <footer class="modalFooter" slot="footer">
                <Button type="default" @click="modal=false">取消</Button>
                <Button type="primary" @click="save">保存</Button>
            </footer>
        </Modal>
    </div>
</template>

<script>
    import axios from '@/libs/api.request'
    import {
        mapGetters,
        mapMutations
    } from 'vuex'
    export default {
        name: 'targets-page',
        data() {
            return {
                loading: false,

                modal: false,
                // 新增修改对象
                formItem: {
                    month_target_money: {
                        1: 0,
                        2: 0,
                        3: 0,
                        4: 0,
                        5: 0,
                        6: 0,
                        7: 0,
                        8: 0,
                        9: 0,
                        10: 0,
                        11: 0,
                        12: 0,
                    }
                },

                // 搜索对象
                search: {
                    department: '全部',
                    year: '',
                    page: 1,
                    rows: 10
                },

                // 数据总条数
                total: 0,

                columns1: [{
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '部门',
                        align: 'center',
                        key: 'department'
                    },
                    {
                        title: '年份',
                        align: 'center',
                        key: 'year'
                    },
                    {
                        title: '一月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money['1'])
                        },
                    },
                    {
                        title: '二月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[2])
                        },
                    },
                    {
                        title: '三月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[3])
                        },
                    },
                    {
                        title: '四月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[4])
                        },
                    },
                    {
                        title: '五月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[5])
                        },
                    },
                    {
                        title: '六月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[6])
                        },
                    },
                    {
                        title: '七月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[7])
                        },
                    },
                    {
                        title: '八月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[8])
                        },
                    },
                    {
                        title: '九月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[9])
                        },
                    },
                    {
                        title: '十月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[10])
                        },
                    },
                    {
                        title: '十一月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[11])
                        },
                    },
                    {
                        title: '十二月',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.month_target_money[12])
                        },
                    },
                    {
                        title: '合计',
                        align: 'center',
                        key: 'sum'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 120,
                        align: 'center',
                        render: (h, params) => {
                            return params.row.id == -1 ||
                                h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'warning',
                                            size: 'default',
                                            icon: 'md-brush',
                                            disabled: this.role > 2
                                        },
                                        style: {
                                            marginRight: '14px',
                                            width: '80px',
                                            height: '30px'
                                        },
                                        on: {
                                            click: () => {
                                                if (this.role > 2) {
                                                    return false
                                                }
                                                this.formItem = {
                                                    wdtmid: params.row.id,
                                                    year: params.row.year,
                                                    department: params.row.department,
                                                    month_target_money: params.row.month_target_money
                                                }
                                                this.modal = true
                                            }
                                        }
                                    }, '编辑'),
                                    // h('Poptip', {
                                    //         props: {
                                    //             confirm: true,
                                    //             title: '你确定要删除吗?',
                                    //             placement: 'top',
                                    //             transfer: true
                                    //         },
                                    //         on: {
                                    //             'on-ok': () => {
                                    //                 if (this.role > 2) {
                                    //                     return false
                                    //                 }
                                    //                 this.deleteFloorByid(params.row.id)
                                    //             }
                                    //         }
                                    //     },
                                    //     [h(
                                    //         'Button', {
                                    //             props: {
                                    //                 type: 'error',
                                    //                 size: 'default',
                                    //                 icon: 'ios-trash',
                                    //                 disabled: this.role > 2
                                    //             },
                                    //             style: {
                                    //                 marginRight: '14px',
                                    //                 width: '80px',
                                    //                 height: '30px'
                                    //             }
                                    //         },
                                    //         '删除'
                                    //     )]
                                    // )
                                ])
                        }
                    }


                ],

                data1: [{
                    month_target_money: {
                        1: '',
                        2: '',
                        3: '',
                        4: '',
                        5: '',
                        6: '',
                        7: '',
                        8: '',
                        9: '',
                        10: '',
                        11: '',
                        12: '',
                    }
                }]
            }
        },
        methods: {
            ...mapGetters(['getDem', 'getRole']),

            // 获取目标金额列表
            getTargetsList() {
                this.loading = true
                let {
                    ...data
                } = this.search

                axios.ajax_post('admin-wr_data_target_money-ajax_get_wdtm', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.total = res.data.data.total_num
                        if (this.data1.length > 0) {
                            this.handleSummary(this.data1)
                        }
                        if (this.data1.length < 1 && this.total > 0) {
                            this.search.page = Math.ceil(this.total / this.search.rows)
                            this.getTargetsList()
                        }
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })

            },

            // 计算合计
            handleSummary(data) {
                const sums = {
                    department: '合计',
                    month_target_money: {
                        1: 0,
                        2: 0,
                        3: 0,
                        4: 0,
                        5: 0,
                        6: 0,
                        7: 0,
                        8: 0,
                        9: 0,
                        10: 0,
                        11: 0,
                        12: 0,
                    },
                    year: '',
                    id: -1,
                    sum: 0
                }
                data.forEach(item => {
                    let sum = 0
                    for (const key in item.month_target_money) {
                        sum += item.month_target_money[key]
                        sums.month_target_money[key] += parseInt(item.month_target_money[key])
                    }
                    item.sum = sum
                    sums.sum += item.sum
                })

                this.data1.push(sums)
            },

            // 新增目标金额
            add() {
                this.formItem = {
                    year: '',
                    department: '',
                    month_target_money: {
                        1: 0,
                        2: 0,
                        3: 0,
                        4: 0,
                        5: 0,
                        6: 0,
                        7: 0,
                        8: 0,
                        9: 0,
                        10: 0,
                        11: 0,
                        12: 0,
                    }
                }
                this.modal = true
            },

            // 搜索
            searchList() {
                this.search.page = 1
                this.search.rows = 10
                this.getTargetsList()
            },

            // 格式化时间
            changeTime(value) {
                this.search.start_time = value[0]
                this.search.end_time = value[1]
            },

            getListByPageSize(rows) {
                this.search.rows = rows
                this.getTargetsList()
            },

            getListByPageNum(page) {
                this.search.page = page
                this.getTargetsList()
            },

            // 保存
            save() {
                let data = this.formItem
                let url = 'admin-wr_data_target_money-ajax_add_wdtm'
                if (data.wdtmid) {
                    url = 'admin-wr_data_target_money-ajax_save_wdtm'
                }
                for (const key in data.month_target_money) {
                    if (data.month_target_money[key].toString().trim() == '') {
                        this.$Notice.error({
                            title: '请输入目标金额',
                            duration: 3
                        })
                        return false
                    }
                }
                axios.ajax_post(url, data).then(res => {
                    if (res.data.code === 1) {
                        this.$Notice.success({
                            title: res.data.msg,
                            duration: 3
                        })
                        this.getTargetsList()
                        this.modal = false
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })
            }
        },
        computed: {
            demList() {
                return this.getDem()
            },
            role() {
                return this.getRole()
            }
        },
        created() {
            this.getTargetsList()
        }
    }
</script>

<style lang='less' scoped>
    .add {
        margin-right: 20px;
    }

    .search_div {
        .search_box {
            width: auto
        }
    }

    .modalFooter {
        display: flex;
        justify-content: flex-end;
    }

    .branchSelect {
        width: 15vw;
    }
</style>